package com.fei.BYSJ.controller;

import com.fei.BYSJ.beans.Data;
import com.fei.BYSJ.entity.User;
import com.fei.BYSJ.service.UserService;
import com.fei.BYSJ.utils.HttpServletRequestUtil;
import com.fei.BYSJ.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Controller()
@RequestMapping(value = "/file",method = RequestMethod.POST)
public class FIleUploadController {
    @Autowired
    UserService userService;

    @RequestMapping(value="/imgfileUpload")
    public @ResponseBody
    Data fileupload(@RequestParam("file")MultipartFile file, Model model, HttpServletRequest request, HttpSession session){
        Map<String,Object> params = HttpServletRequestUtil.getRequestParams(request);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String res = sdf.format(new Date());
        User user = (User) session.getAttribute("user");
        if (user.getUsertype()==0&& params.get("id").toString()!=null){
            user = userService.findOne(Integer.parseInt(params.get("id").toString()));

        }
        // uploads文件夹位置
        String rootPath = request.getSession().getServletContext().getRealPath("resources/uploads/");
        System.out.println(request.getServletContext().getRealPath("resources/uploads/"));
        // 原始名称
        String originalFileName = file.getOriginalFilename();
        // 新文件名
        String newFileName = "userheadimg" + res + originalFileName.substring(originalFileName.lastIndexOf("."));
        // 创建年月文件夹
        Calendar date = Calendar.getInstance();
        File dateDirs = new File(user.getUseraccount());

        // 新文件
        File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
        // 判断目标文件所在目录是否存在
        if( !newFile.getParentFile().exists()) {
            // 如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        System.out.println(newFile);
        // 将内存中的数据写入磁盘
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 完整的url
        String fileUrl = "resources/uploads/" +user.getUseraccount() + "/" + newFileName;
        user.setUserimg(fileUrl);
        userService.uploadUser(user);
        System.out.println(">>>>>>>>>>>"+fileUrl);
        return ResultData.success("成功","");



    }
    @RequestMapping(value="/erfileUpload")
    public @ResponseBody
    Data erfileupload(@RequestParam("file")MultipartFile file, Model model, HttpServletRequest request, HttpSession session){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String res = sdf.format(new Date());
        User user = (User) session.getAttribute("user");
        // uploads文件夹位置
        String rootPath = request.getSession().getServletContext().getRealPath("resources/uploads/");
        System.out.println(request.getServletContext().getRealPath("resources/uploads/"));
        // 原始名称
        String originalFileName = file.getOriginalFilename();
        // 新文件名
        String newFileName = "userERimg" + res + originalFileName.substring(originalFileName.lastIndexOf("."));
        // 创建年月文件夹
        Calendar date = Calendar.getInstance();
        File dateDirs = new File(user.getUseraccount());

        // 新文件
        File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
        // 判断目标文件所在目录是否存在
        if( !newFile.getParentFile().exists()) {
            // 如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        System.out.println(newFile);
        // 将内存中的数据写入磁盘
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 完整的url
        String fileUrl = "resources/uploads/" +user.getUseraccount() + "/" + newFileName;
        user.setUserewimg(fileUrl);
        userService.uploadUser(user);
        System.out.println(">>>>>>>>>>>"+fileUrl);
        return ResultData.success("成功","");



    }

}
